/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"
#include "systick.h"
#include "..//BSP/tiny-macro-os/tiny-macro-os.h"


/* Global typedef */

/* Global define */

/* Global Variable */


OS_TASK(os_test2)
{
    OS_TASK_START(os_test2);
    /* 禁止在OS_TASK_START和OS_TASK_END之间使用switch */
    while (1)
    {
        printf("task2 1\n");
        OS_TASK_WAITX(OS_SEC_TICKS * 3 / 10);
        printf("task2 2\n");
        OS_TASK_WAITX(OS_SEC_TICKS * 4 / 10);
        printf("task2 3\n");
        OS_TASK_WAITX(OS_SEC_TICKS * 5 / 10);
    }
    OS_TASK_END(os_test2);
}

OS_TASK(os_test1, void)
{
    OS_TASK_START(os_test1);
    /* 禁止在OS_TASK_START和OS_TASK_END之间使用switch */
    while (1)
    {
        printf("task1 start\n");
        OS_TASK_WAITX(OS_SEC_TICKS * 3);

        printf("suspend task2\n");
        /*挂起任务，保留任务状态*/
        OS_TASK_SUSPEND_ANOTHER(os_test2);
        OS_TASK_WAITX(OS_SEC_TICKS * 3);
        printf("restart task2\n");
        /* 必须已经停止的任务才可以重启，不然无效 */
        OS_TASK_RESTART_ANOTHER(os_test2, 0);
        OS_TASK_WAITX(OS_SEC_TICKS * 3);

        printf("exit task2\n");
        /*退出任务，清除任务状态*/
        OS_TASK_EXIT_ANOTHER(os_test2);
        OS_TASK_WAITX(OS_SEC_TICKS * 3);
        printf("call task2\n");
        OS_TASK_CALL_ANOTHER(os_test2);
        OS_TASK_WAITX(OS_SEC_TICKS * 3);
    }
    OS_TASK_END(os_test1);
}


/**
 * @fn      main
 * @brief   Main program.
 * @return  none
 */
int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	SystemCoreClockUpdate();
	Delay_Init();
	USART_Printf_Init(115200);
	systick_config();

	#if 0
	printf("SystemClk:%d\r\n",SystemCoreClock);
	printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
	printf("This is printf example\r\n");
	#endif

	OS_INIT_TASKS();

	while(1)
    {
		/* 所有的主任务都需要手动在main函数的while(1)中调用 */
        OS_RUN_TASK(os_test1);
        OS_RUN_TASK(os_test2);
		#if 0
		delay_1ms(1000);
		printf("delay_1ms\r\n");
		#endif
	}
}

